<?php


namespace App\Repositories\Log;


use App\Models\BaseModel;
use App\Models\Log\Login;
use App\Repositories\BaseRepository;
use App\Utils\Output;
use App\Utils\Pager;
use App\Utils\QueryHelper;

class LoginRepository extends BaseRepository
{

    function adminPager($params)
    {
        extract($params);
        $query = Login::query();
        $query->leftJoin('system__users', 'system__users.id', '=', 'log__logins.user_id');
        if (!empty($keyword)) {
            QueryHelper::like($query, 'system__users.username|system__users.mobile',  $keyword);
        }
        if (!empty($from)) {
            $query->where('log__logins.created_at', '>=', $from);
        }
        if (!empty($to)) {
            $query->where('log__logins.created_at', '<=', $to . ' 23:59:59');
        }

        $total = $query->count();
        $query->select(
            'log__logins.id',
            'log__logins.ip',
            'log__logins.app_id as app_text',
            'log__logins.token',
            'system__users.username',
            'system__users.mobile',
            'log__logins.created_at'
        );
        $lis = Pager::setPageParams($query, $params)->get();

        return Output::pager($lis, $total);
    }

    function getModel(): BaseModel
    {
        return new Login();
    }
}